// Thud - a dynamic object-oriented language
// Copyright (C) 2009,2010 David H. Hovemeyer <david.hovemeyer@gmail.com>

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

/*
 * Lexer2.h
 *
 *  Created on: Dec 11, 2009
 *      Author: dhovemey
 */

#ifndef LEXER2_H_
#define LEXER2_H_

#include "fe/Location.h"
class Token2;
class Reader;
class PushbackReader;

/**
 * Lexical analyzer for Thud source files.
 */
class Lexer2 {
private:
	PushbackReader *m_reader;
	Token2 *m_prevToken;
	Token2 *m_nextToken;
	int m_curLine;

	Lexer2(const Lexer2 &);
	Lexer2 operator=(const Lexer2 &);

public:
	/**
	 * Constructor.
	 *
	 * @param readerToAdopt Reader that will be adopted to do the reading of the source text
	 */
	Lexer2(Reader *readerToAdopt);

	/** Destructor. */
	virtual ~Lexer2();

	/**
	 * Peek ahead to next token without consuming it.
	 * @return the next token, or null if lexer is at end of source text
	 */
	Token2* peek();

	/**
	 * Consume next token an return it.
	 * Throws exception if lexer is at end of source text.
	 * @return the next token
	 */
	Token2* next();

	/**
	 * Get the lexer's current location in the source text.
	 */
	Location getCurrentLocation() const;

private:
	long read();
	long readNextNonWS();
	void skipComment();
	void pushBack(wchar_t c);
	void readToken(
			wchar_t first,
			bool (*validContinuation)(wchar_t),
			const Location &startLoc,
			int (*chooseType)(const std::wstring &lexeme));
	void readStringLiteral(wchar_t first, const Location &startLoc);
	void createToken(int type, const std::wstring &lexeme, const Location &loc);
};

#endif /* LEXER2_H_ */
